home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / brailler-04b-c / brlr ƒ / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  8.2 KB  |  314 lines  |  [TEXT/MMCC]

  1. #include "menus.h"
  2. #include "graphics.h"
  3. #include "help.h"
  4. #include "environment.h"
  5. #include "print meat.h"
  6. #include "brlr load-save.h"
  7. #include "brlr.h"
  8. #include "text twiddling.h"
  9. #include "graphics dispatch.h"
  10. #include "window layer.h"
  11. #include "program globals.h"
  12.  
  13. static    MenuHandle        gAppleMenu;
  14. static    MenuHandle        gFileMenu;
  15. static    MenuHandle        gEditMenu;
  16. static    MenuHandle        gOptionsMenu;
  17.  
  18. enum
  19. {
  20.     appleMenu = 400, fileMenu, editMenu, optionsMenu,
  21.     
  22.     aboutItem = 1, aboutMSGItem, otherMSGItem, helpPointerItem,
  23.     
  24.     newItem = 1, openItem, file_unused1, closeItem, saveItem, saveAsItem, file_unused2,
  25.         pageSetupItem, printItem, file_unused3, quitItem,
  26.     
  27.     undoItem = 1, edit_unused0, cutItem, copyItem, pasteItem, clearItem, edit_unused1, selectAllItem,
  28.     
  29.     useBrailleItem = 1, useLettersItem, useGrade2Item
  30. };
  31.  
  32. /*-----------------------------------------------------------------------------------*/
  33. /* internal stuff for menus.c                                                        */
  34.  
  35. static    void HandleAppleMenu(short menuItem);
  36. static    void HandleFileMenu(short menuItem);
  37. static    void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough);
  38. static    void HandleOptionsMenu(short menuItem);
  39. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition);
  40.  
  41. Boolean InitTheMenus(void)
  42. {
  43.     Handle            MBARHandle;
  44.     
  45.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  46.         return FALSE;
  47.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  48.     
  49.     if ((gAppleMenu=GetMHandle(appleMenu))==0L)    /* GetNewMBar also got menu handles of */
  50.         return FALSE;
  51.     if ((gFileMenu=GetMHandle(fileMenu))==0L)    /* every menu it includes, so just */
  52.         return FALSE;
  53.     if ((gEditMenu=GetMHandle(editMenu))==0L)    /* grab these handles and assign them */
  54.         return FALSE;
  55.     if ((gOptionsMenu=GetMHandle(optionsMenu))==0L)
  56.         return FALSE;
  57.     
  58.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  59.     
  60.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  61.     DrawMenuBar();                                /* draws the actual menu bar */
  62.     
  63.     return TRUE;
  64. }
  65.  
  66. void AdjustMenus(void)
  67. {
  68.     short            kind;
  69.     WindowPtr        front, frontDoc, mainWindow;
  70.     
  71.     front=FrontWindow();
  72.     kind=front ? ((WindowPeek)front)->windowKind : 0;
  73.     frontDoc=GetFrontDocumentWindow();
  74.     mainWindow=GetIndWindowPtr(kMainWindow);
  75.     
  76.     EDItem(gAppleMenu, 0, TRUE);
  77.     EDItem(gAppleMenu, aboutItem, TRUE);
  78.     EDItem(gAppleMenu, aboutMSGItem, TRUE);
  79.     EDItem(gAppleMenu, otherMSGItem, TRUE);
  80.     EDItem(gAppleMenu, helpPointerItem, TRUE);
  81.     
  82.     EDItem(gFileMenu, 0, TRUE);
  83.     EDItem(gFileMenu, newItem, mainWindow==0L);
  84.     EDItem(gFileMenu, openItem, mainWindow==0L);
  85.     EDItem(gFileMenu, closeItem, front!=0L);
  86.     EDItem(gFileMenu, saveItem, (mainWindow!=0L) && (WindowHasLayer(mainWindow)) &&
  87.         (WindowIsModifiedQQ(mainWindow)));
  88.     EDItem(gFileMenu, saveAsItem, mainWindow!=0L);
  89.     EDItem(gFileMenu, pageSetupItem, TRUE);
  90.     EDItem(gFileMenu, printItem, (frontDoc!=0L) && (WindowHasLayer(frontDoc)) &&
  91.         (GetWindowTE(frontDoc)!=0L));
  92.     EDItem(gFileMenu, quitItem, TRUE);
  93.     
  94.     EDItem(gEditMenu, 0, TRUE);
  95.     EDItem(gEditMenu, undoItem, kind<0);
  96.     EDItem(gEditMenu, cutItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  97.     EDItem(gEditMenu, copyItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  98.     EDItem(gEditMenu, pasteItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyTextInScrapQQ()));
  99.     EDItem(gEditMenu, clearItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  100.     EDItem(gEditMenu, selectAllItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyTextQQ(frontDoc)));
  101.     
  102.     EDItem(gOptionsMenu, 0, TRUE);
  103.     EDItem(gOptionsMenu, useBrailleItem, mainWindow!=0L);
  104.     EDItem(gOptionsMenu, useLettersItem, mainWindow!=0L);
  105.     EDItem(gOptionsMenu, useGrade2Item, mainWindow!=0L);
  106.     
  107.     SetItemMark(gOptionsMenu, useBrailleItem, (gGrade==0) ? '◊' : noMark);
  108.     SetItemMark(gOptionsMenu, useLettersItem, (gGrade==1) ? '◊' : noMark);
  109.     SetItemMark(gOptionsMenu, useGrade2Item, (gGrade==2) ? '◊' : noMark);
  110. }
  111.  
  112. void HandleMenu(long mSelect)
  113. {
  114.     short            menuID = HiWord(mSelect);
  115.     short            menuItem = LoWord(mSelect);
  116.     
  117.     switch (menuID)
  118.     {
  119.         case appleMenu:
  120.             HandleAppleMenu(menuItem);
  121.             break;
  122.         case fileMenu:
  123.             HandleFileMenu(menuItem);
  124.             break;    
  125.         case editMenu:
  126.             HandleEditMenu(menuItem, FALSE);
  127.             break;
  128.         case optionsMenu:
  129.             HandleOptionsMenu(menuItem);
  130.             break;
  131.     }
  132. }
  133.  
  134. void DoTheCloseThing(WindowPeek theWindow)
  135. /* a standard close procedure, called when "close" is chosen from File menu and when
  136.    a window is closed through its close box */
  137. {
  138.     short            kind;
  139.     
  140.     if (theWindow==0L)
  141.         return;
  142.     
  143.     kind = theWindow ? theWindow->windowKind : 0;
  144.     if (kind<0)        /* DA window or other system window */
  145.         CloseDeskAcc(kind);
  146.     else
  147.     {
  148.         if (WindowHasLayer((WindowPtr)theWindow))
  149.             CloseTheWindow((WindowPtr)theWindow);
  150.         else
  151.             DisposeWindow((WindowPtr)theWindow);
  152.         
  153.         AdjustMenus();
  154.     }
  155. }
  156.  
  157. void HandleAppleMenu(short menuItem)
  158. {
  159.     GrafPtr        savePort;
  160.     Str255        name;
  161.     
  162.     switch (menuItem)
  163.     {
  164.         case aboutItem:
  165.             if (!IndWindowExistsQQ(kAboutWindow))
  166.                 OpenTheIndWindow(kAboutWindow);
  167.             else
  168.                 MySelectWindow(GetIndWindowPtr(kAboutWindow));
  169.             break;
  170.         case aboutMSGItem:
  171.             if (!IndWindowExistsQQ(kAboutMSGWindow))
  172.                 OpenTheIndWindow(kAboutMSGWindow);
  173.             else
  174.                 MySelectWindow(GetIndWindowPtr(kAboutMSGWindow));
  175.             break;
  176.         case otherMSGItem:
  177.             if (!IndWindowExistsQQ(kOtherMSGWindow))
  178.                 OpenTheIndWindow(kOtherMSGWindow);
  179.             else
  180.                 MySelectWindow(GetIndWindowPtr(kOtherMSGWindow));
  181.             break;
  182.         case helpPointerItem:
  183.             if (!IndWindowExistsQQ(kHelpWindow))
  184.                 OpenTheIndWindow(kHelpWindow);
  185.             else
  186.                 MySelectWindow(GetIndWindowPtr(kHelpWindow));
  187.             break;
  188.         default:
  189.             if (menuItem > helpPointerItem+1)
  190.             {
  191.                 GetPort(&savePort);
  192.                 GetItem(gAppleMenu, menuItem, name);
  193.                 OpenDeskAcc(name);
  194.                 SetPort(savePort);
  195.             }
  196.             break;
  197.     }
  198. }
  199.  
  200. void HandleFileMenu(short menuItem)
  201. {
  202.     WindowPtr            theWindow;
  203.     TEHandle            hTE;
  204.     
  205.     switch (menuItem)
  206.     {
  207.         case newItem:
  208.             OpenTheIndWindow(kMainWindow);
  209.             break;
  210.         case openItem:
  211.             LoadSaveDispatch(TRUE, FALSE);
  212.             break;
  213.         case closeItem:
  214.             if ((theWindow=GetFrontDocumentWindow())!=0L)
  215.                 DoTheCloseThing((WindowPeek)theWindow);
  216.             else
  217.                 DoTheCloseThing((WindowPeek)FrontWindow());
  218.             break;
  219.         case saveItem:
  220.             LoadSaveDispatch(FALSE, TRUE);
  221.             break;
  222.         case saveAsItem:
  223.             LoadSaveDispatch(FALSE, FALSE);
  224.             break;
  225.         case pageSetupItem:
  226.             RemoveHilitePatch();
  227.             DoThePageSetup();
  228.             InstallHilitePatch();
  229.             break;
  230.         case printItem:
  231.             theWindow=GetFrontDocumentWindow();
  232.             if (WindowHasLayer(theWindow))
  233.             {
  234.                 hTE=GetWindowTE(theWindow);
  235.                 if (hTE!=0L)
  236.                 {
  237.                     RemoveHilitePatch();
  238.                     PrintText(hTE);
  239.                     InstallHilitePatch();
  240.                 }
  241.             }
  242.             else SysBeep(7);
  243.             break;
  244.         case quitItem:
  245.             gDone=ShutDownTheProgram();
  246.             break;
  247.     }
  248. }
  249.  
  250. void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough)
  251. {
  252.     short            index;
  253.     WindowPtr        frontDoc;
  254.     enum DispatchError    resultCode;
  255.     
  256.     if (!alreadyPassedThrough)
  257.         index=(gFrontWindowIsOurs) ? gFrontWindowIndex : -1;
  258.     else
  259.     {
  260.         frontDoc=GetFrontDocumentWindow();
  261.         index=(frontDoc!=0L) ? GetWindowIndex(frontDoc) : -1;
  262.     }
  263.     
  264.     if (index>=0)
  265.     {
  266.         switch (menuItem)
  267.         {
  268.             case undoItem:        resultCode=UndoDispatch(index);            break;
  269.             case cutItem:        resultCode=CutDispatch(index);            break;
  270.             case copyItem:        resultCode=CopyDispatch(index);            break;
  271.             case pasteItem:        resultCode=PasteDispatch(index);        break;
  272.             case clearItem:        resultCode=ClearDispatch(index);        break;
  273.             case selectAllItem:    resultCode=SelectAllDispatch(index);    break;
  274.             default:            resultCode=kSuccess;                    break;
  275.         }
  276.         
  277.         if ((resultCode==kPassThrough) && (!alreadyPassedThrough))
  278.             HandleEditMenu(menuItem, TRUE);
  279.     }
  280.     else SystemEdit(menuItem-1);
  281. }
  282.  
  283. void HandleOptionsMenu(short menuItem)
  284. {
  285.     switch (menuItem)
  286.     {
  287.         case useBrailleItem:
  288.             gGrade=0;
  289.             if (IndWindowExistsQQ(kFloatingWindow))
  290.                 CloseTheWindow(GetIndWindowPtr(kFloatingWindow));
  291.             break;
  292.         case useLettersItem:
  293.             gGrade=1;
  294.             if (IndWindowExistsQQ(kFloatingWindow))
  295.                 CloseTheWindow(GetIndWindowPtr(kFloatingWindow));
  296.             break;
  297.         case useGrade2Item:
  298.             gGrade=2;
  299.             if (!IndWindowExistsQQ(kFloatingWindow))
  300.                 OpenTheIndWindow(kFloatingWindow);
  301.             break;
  302.     }
  303.     
  304.     AdjustMenus();
  305. }
  306.  
  307. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition)
  308. {
  309.     if (theCondition)
  310.         EnableItem(theMenu, theItem);
  311.     else
  312.         DisableItem(theMenu, theItem);
  313. }
  314.